package com.qabaco.odondata.mp;


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.ApacheHttpClient;

import java.util.HashMap;

import javax.ws.rs.POST;

import mercadopago.MP;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.sun.jersey.api.client.WebResource.Builder;

public class Test {
    public Test() {
        super();
    }

    public static void main(String[] args) {
        Test test = new Test();
      
        test.usuarioPrueba();
    }
    
    public void usuarioPrueba(){
        try{
        String clientId = "8359489755050931";
        String clientSecret = "3xjt9TVk2XzguEcGRzZrfep4S7yMN2aP";
          System.clearProperty("javax.net.ssl.trustStore");
       /* HashMap<String, Object> appClientValues = new HashMap<String, Object>();
        appClientValues.put("grant_type", "client_credentials");
        appClientValues.put("client_id", clientId);
        appClientValues.put("client_secret", clientSecret);*/
        MP mp = new MP (clientId, clientSecret);
          JSONObject siteId = new JSONObject ();
              siteId.put("site_id", "MLA"); //"{'site_id':'MLA'}"
        JSONObject jSONObject = Test.post("/users/test_user?access_token=" + mp.getAccessToken(), siteId);
        //  curl -X POST -H "Content-Type: application/json" -d  
        
        Object z = null;
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
    
    }
    
    public void probar(){
      try {
           // MP mp = new MP("CLIENT_ID", "CLIENT_SECRET");
            //String accessToken = mp.getAccessToken();

            //System.out.println(accessToken);
           
            String clientId = "8359489755050931";
            String clientSecret = "3xjt9TVk2XzguEcGRzZrfep4S7yMN2aP";
  
            MP mp = new MP (clientId, clientSecret);

           mp.getAccessToken();

            JSONObject preference = mp.createPreference("{'items':[{'title':'sdk-java','quantity':1,'currency_id':'ARS','unit_price':10.5}]}");
            //JSONObject preference = mp.createPreference("{items:[{'title':'sdk-java','quantity':1,'currency_id':'ARS','unit_price':10.5}]}");
  
            String checkoutURL = preference.getJSONObject("response").getString("init_point");    
          
            Object z = null;
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
    }
    
    
  private static final String API_BASE_URL = "https://api.mercadolibre.com";
      public static final String MIME_JSON = "application/json";
      public static final String MIME_FORM = "application/x-www-form-urlencoded";
      
      private static JSONObject exec (String method, String uri, Object data, String contentType) throws JSONException {
        ClientResponse apiResult = buildRequest(API_BASE_URL+uri, contentType).method(method, ClientResponse.class, data);
        int apiHttpCode = apiResult.getStatus();
        
        JSONObject response = new JSONObject ();
        response.put("status", apiHttpCode);
        response.put("response", apiResult.getEntity(JSONObject.class));
        
        return response;
      }
      
      public static JSONObject get (String uri) throws JSONException {
        return get(uri, MIME_JSON);
      }
      
      public static JSONObject get (String uri, String contentType) throws JSONException {
        return exec ("GET", uri, null, contentType);
      }
      
      public static JSONObject post (String uri, Object data) throws JSONException {
        return post(uri, data, MIME_JSON);
      }
      
      public static JSONObject post (String uri, Object data, String contentType) throws JSONException {
        return exec ("POST", uri, data, contentType);
      }
      
      public static JSONObject put (String uri, Object data) throws JSONException {
        return put(uri, data, MIME_JSON);
      }
      
      public static JSONObject put (String uri, Object data, String contentType) throws JSONException {
        return exec ("PUT", uri, data, contentType);
      }
      
      private static Builder buildRequest (String resourceUrl, String contentType) {
        // Obtenemos cliente Http de Apache
        Client client = ApacheHttpClient.create();

        WebResource resource = client.resource(resourceUrl);
        Builder req = resource.type(contentType).accept("application/json");
        req.header("User-Agent", "MercadoPago Java SDK v"+MP.version);
        return req;
      }
}
